 /*
 * UMLSynch - VPSDE plugin part
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.vpsde.plugins.menuitem.patterns;

import java.awt.event.ActionEvent;
import java.util.Iterator;

import com.vp.plugin.ApplicationManager;
import com.vp.plugin.action.VPAction;
import com.vp.plugin.action.VPContext;
import com.vp.plugin.action.VPContextActionController;
import com.vp.plugin.diagram.IDiagramElement;
import com.vp.plugin.diagram.IDiagramUIModel;
import com.vp.plugin.diagram.shape.INoteUIModel;
import com.vp.plugin.model.INOTE;

public class ActionController implements VPContextActionController {

	private static String PatternDescriptor = "Pattern Descriptor";
	private static String PatternType = "type";
	private static String PatternHasSubDiagram = "has sub diagram";
	private static String PatternDiagramName = "diagram name";
	private static String PatternNumverOfValues = "number of values";
	private static String PatterValueFormat = "value%d";
	private static String ParamAnyValueStringPattern = "[a-zA-Z0-9]*";
	
	private static String PARAM_PATTER_TYPE_SYNTAX_BASED = "syntax based";
	private static String PARAM_PATTER_TYPE_RELATIONS_BASED = "relations based";
	
	private boolean SyntaxBasedPattern = true;
	private boolean HasSubDiagram = false;
	private int NumverOfValues = -1;
	private String[] Values = null;
	private String DiagramName = null;
	
	
	@Override
	public void performAction(VPAction arg0, VPContext arg1, ActionEvent arg2) {
		FindPatternDescriptor(arg1);
	}
	
	public String FindPatternDescriptor(VPContext arg1) {
		
		Iterator iter = arg1.getDiagram().diagramElementIterator();
		while (iter.hasNext()) {
			IDiagramElement next = (IDiagramElement) iter.next();
			if (next instanceof INoteUIModel) {
				INoteUIModel note = (INoteUIModel)next;
				if (note.getModelElement().getHTMLDocumentation().contains(PatternDescriptor)) {
					return note.getModelElement().getHTMLDocumentation();
				}
				
			}
		}
		
		return null;
	}
	
	public boolean ParsePatternDescriptor(String desc) {
		String[] items = desc.split("\n");
		if ((items == null) || (items.length < 5)) {
			return false;
		}
		
		if (!items[0].equalsIgnoreCase(PatternDescriptor)) {
			return false;
		}

		for (int i =1; i<items.length; ++i) {
			String[] params = items[i].split(":");
			if (params.length != 2) {
				return false;
			}
			switch (i) {
			case 1:
				if (params[0].equals(this.PatternType)) {
					if (params[1].equalsIgnoreCase(this.PARAM_PATTER_TYPE_SYNTAX_BASED)) {
						SyntaxBasedPattern = true;
					}
				}
				break;
			case 2:
				if (params[0].equals(this.PatternHasSubDiagram)) {
					if (params[1].equalsIgnoreCase("true")) {
						HasSubDiagram = true;
					}
				}
				break;
			case 3:
				if (params[0].equals(PatternDiagramName)) {
					DiagramName = params[1];
				}
				break;
			case 4:
				if (params[0].equals(this.PatternNumverOfValues)) {
					NumverOfValues = Integer.parseInt(params[1]);
					if ((NumverOfValues<0) || (NumverOfValues>items.length-i-1)) {
						return false;
					}
				}
				
				Values = new String[NumverOfValues];
				
				for (int v=0; v<NumverOfValues; ++v) {
					params = items[i+v+1].split(":");
					if (params.length != 2) {
						return false;
					}
					Values[v] = params[1];
					i+= NumverOfValues;
				}
				
				break;
			default:
				break;

			}
		}
		return true;
	}

	private IDiagramElement GetTemplateElement(IDiagramUIModel diagram, int indx) {
		String index = "{" + indx + "}";  // {i}
		Iterator iter = diagram.diagramElementIterator();
		while (iter.hasNext()) {
			IDiagramElement next = (IDiagramElement) iter.next();
			if (next != null) {
				// Check index in comments				
			}
		}
		
		return null;		
	}
	
	@Override
	public void update(VPAction arg0, VPContext arg1) {
		// TODO Auto-generated method stub

	}

}
